package me.pplay.playtalk.activity;

import java.util.ArrayList;
import java.util.List;

import me.pplay.playtalk.ApplicationTrans;
import me.pplay.playtalk.R;
import me.pplay.playtalk.adapter.ConversationFriendAdapter;
import me.pplay.playtalk.protocol.TpChat;
import me.pplay.playtalk.protocol.bean.TPBuddyinfo;
import me.pplay.playtalk.util.AsyncImageLoader;
import me.pplay.playtalk.util.BuddyInfoUtil;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;

public class ChooseFriendsActivity extends BaseActivity implements OnClickListener{

	private ListView listView;
	private EditText searchTv;
	private ImageButton btn_back;
	private RelativeLayout layout_bar;
	private LinearLayout layout_cancel, layout_ok, layout_bar_search;
	
	private TpChat tpChat;
	private List<TPBuddyinfo> buddyinfoList;
	private List<TPBuddyinfo> searchList;
	private ConversationFriendAdapter adapter;
	private AsyncImageLoader imageLoader;
	private final int SETADAPTER 			= 1;
	private final int ENABLED_OK			= 8;
	
	Handler handler = new Handler(){

		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			super.handleMessage(msg);
			switch (msg.what) {
			
			case SETADAPTER:
				if(adapter == null){
					adapter = new ConversationFriendAdapter(ChooseFriendsActivity.this, searchList, imageLoader, handler);
					listView.setAdapter(adapter);
				}else{
					adapter.notifyDataSetChanged();
				}
				break;
			case ENABLED_OK:
				showLayout();
				break;
			}
		}
		
	};
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		findViews();
		initDatas();
		clickEvents();
	}
	
	private void findViews(){
		listView			= (ListView)findViewById(R.id.listView);
		searchTv			= (EditText)findViewById(R.id.searchTv);
		btn_back			= (ImageButton)findViewById(R.id.btn_back);
		layout_bar			= (RelativeLayout)findViewById(R.id.layout_bar);
		layout_bar_search	= (LinearLayout)findViewById(R.id.layout_bar_search);
		layout_cancel		= (LinearLayout)findViewById(R.id.layout_cancel);
		layout_ok			= (LinearLayout)findViewById(R.id.layout_ok);
	}
	
	private void initDatas(){
		searchTv.addTextChangedListener(new MyTextWatcher());
		ApplicationTrans appState = (ApplicationTrans) getApplicationContext();
		tpChat 				= appState.getTpChat();
		buddyinfoList		= tpChat.tp_chat_getfriendslist();
		if(buddyinfoList == null)
			return;
		imageLoader			= new AsyncImageLoader();
		searchList 			= new ArrayList<TPBuddyinfo>();
		for (int i = 0; i < buddyinfoList.size(); i++) {
			searchList.add(buddyinfoList.get(i));
		}
		handler.sendEmptyMessage(SETADAPTER);
	}
	
	private void clickEvents(){
		btn_back.setOnClickListener(this);
		layout_cancel.setOnClickListener(this);
		layout_ok.setOnClickListener(this);
	}
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.layout_cancel:
		case R.id.btn_back:
			ChooseFriendsActivity.this.finish();
			break;

		default:
			break;
		}
	}

	private void showLayout(){
		if(layout_bar_search.getVisibility() == View.GONE){
			layout_bar_search.setVisibility(View.VISIBLE);
			layout_bar.setVisibility(View.GONE);
		}
		layout_ok.setEnabled(adapter.selectedList.size() > 0);
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
	}

	class MyTextWatcher implements TextWatcher{

		@Override
		public void afterTextChanged(Editable s) {
			
		}

		@Override
		public void beforeTextChanged(CharSequence s, int start, int count, int after) {
		}

		@Override
		public void onTextChanged(CharSequence s, int start, int before, int count) {
			if(buddyinfoList == null)
				return;
			showLayout();
			searchList.clear();
			int countF = buddyinfoList.size();
			for(int i = 0; i<countF; i++){
				TPBuddyinfo tpinfo = buddyinfoList.get(i);
				if(BuddyInfoUtil.getAlias(tpinfo).toLowerCase().indexOf(s.toString().trim().toLowerCase()) != -1){
					searchList.add(tpinfo);
				}
			}
			handler.sendEmptyMessage(SETADAPTER);
		}
		
	}
	

}
